package com.lanou.musicproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.SongList;
import com.lanou.musicproject.tools.ImageUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/21.
 * 歌单页适配器
 */
public class SongListAdapter extends BaseAdapter {
    private List<SongList.ContentEntity> data;//获取图片(pic_300),聆听数(listenum),标签(tag),标题(title)
    private Context mContext;//上下文

    //有参构造方法
    public SongListAdapter(Context c, List<SongList.ContentEntity> data) {
        this.data = data;
        mContext = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (data != null && data.size() > 0)
            return data.size();
        else
            return 0;
    }

    //获得当前行布局的坐标
    @Override
    public SongList.ContentEntity getItem(int position) {
        if (data != null && data.size() > 0)
            return data.get(position);
        else
            return null;
    }

    //获得当前行布局ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    //获得行布局内容
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.songlist_gridview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SongList.ContentEntity content = getItem(position);
        if (content != null) {
            Picasso.with(mContext).load(content.getPic_300()).into(holder.backgroundIV);
            holder.listenTV.setText(content.getListenum());
            holder.songTitleTV.setText(content.getTitle());
            holder.songTagTV.setText(content.getTag());
        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'songlist_gridview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    //初始化组件
    static class ViewHolder {
        @Bind(R.id.backgroundIV)
        ImageUtils backgroundIV;
        @Bind(R.id.listenTV)
        TextView listenTV;
        @Bind(R.id.songTitleTV)
        TextView songTitleTV;
        @Bind(R.id.songTagTV)
        TextView songTagTV;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'songlist_gridview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */

}
